// https://www.luogu.com.cn/problem/P8306

#include <iostream>
#include <vector>
using namespace std;

struct TrieNode {
    int children[66] = {};
    int cnt = 0;
};

constexpr int MAX_ARRAY_SIZE = 3e6+5;
TrieNode nodes[MAX_ARRAY_SIZE];
int idx = 0;
int start = 0;


// 计算 char 类型对应的数值，题目提到 ch 只有三种可能
// 如果不这样处理，而是直接使用 128 ，发现内存使用超出了限制，服了
int calc(const char ch) {
    if(ch >= 'A' && ch <= 'Z') {
        return ch - 'A' + 1;
    }
    if( ch >= 'a' && ch <= 'z') {
        return ch - 'a' + 27;
    }

    return ch - '0' + 53;
}

void Insert(const string& word) {
    auto p = start;
    for (const char ch: word) {
        const int t = calc(ch);
        if (nodes[p].children[t] == 0) {
            nodes[p].children[t] = ++idx;
        }
        p = nodes[p].children[t];
        nodes[p].cnt++;
    }
}

int Search(const string& word) {
    auto p = start;
    for (const char ch: word) {
        const int t = calc(ch);

        if (nodes[p].children[t] == 0) {
            return 0;
        }
        p = nodes[p].children[t];
    }
    return nodes[p].cnt;
}


int main() {
    int T, n, q;
    string word;
    scanf("%d", &T);
    for (int i=0; i<T; i++) {
        scanf("%d %d", &n, &q);
        for (int j=0; j < n; j++) {
            cin >> word;
            Insert(word);
        }

        for (int j=0; j<q; j++) {
            cin >> word;
            const int t = Search(word);
            printf("%d\n", t);
        }
        idx++;
        start=idx;
    }
    return 0;
}